using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qBAAUF = from m in mtemp.BAAUF select m;
       DBMapper.MapBAAUF(qBAAUF.ToList(), true);
       */
        public List<BAAUF> MapBAAUF(XElement item2, bool mergedb)
        {
            List<BAAUF> result = new List<BAAUF>();
            var item4 = from item3 in item2.Elements()
                        select new BAAUF
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            BAMANDANT = (string)item3.Element("BAMANDANT"),
                            BAAUFTYP = (string)item3.Element("BAAUFTYP"),
                            BAAUFKRS = (string)item3.Element("BAAUFKRS"),
                            BAAUFLFD = (string)item3.Element("BAAUFLFD"),
                            BAAUFPOS = (string)item3.Element("BAAUFPOS"),
                            BASA = (string)item3.Element("BASA"),
                            BAPOS = (string)item3.Element("BAPOS"),
                            BAAFOLGE = (string)item3.Element("BAAFOLGE"),
                            BADATVON = (string)item3.Element("BADATVON"),
                            BAKART = (string)item3.Element("BAKART"),
                            BAKVAR = (string)item3.Element("BAKVAR"),
                            BAKFARBE = (string)item3.Element("BAKFARBE"),
                            BAMTR1 = (string)item3.Element("BAMTR1"),
                            BAANZ1 = (string)item3.Element("BAANZ1"),
                            BAMTR2 = (string)item3.Element("BAMTR2"),
                            BAANZ2 = (string)item3.Element("BAANZ2"),
                            BAMTR3 = (string)item3.Element("BAMTR3"),
                            BAANZ3 = (string)item3.Element("BAANZ3"),
                            BAMTR4 = (string)item3.Element("BAMTR4"),
                            BAANZ4 = (string)item3.Element("BAANZ4"),
                            BAMTR5 = (string)item3.Element("BAMTR5"),
                            BAANZ5 = (string)item3.Element("BAANZ5"),
                            BAMTR6 = (string)item3.Element("BAMTR6"),
                            BAMTR7 = (string)item3.Element("BAMTR7"),
                            BAANZ7 = (string)item3.Element("BAANZ7"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapBAAUF(result, mergedb);
          return result;
        }
        ///DBMapper.MapBAAUF(mtemp.BAAUF.ToList<BAAUF>(), true);        
       public void MapBAAUF(List<BAAUF> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from BAAUF row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.BAAUF.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.BAAUF.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.BAAUF.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "BAAUF")]
    public class BAAUF : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _BAMANDANT="0";
        [Column]
        public string BAMANDANT
        {
            get
            {
                return _BAMANDANT;
            }
            set
            {
                if (value != _BAMANDANT)
                {
                     _BAMANDANT = value;
                    NotifyPropertyChanged("BAMANDANT");
                }
            }
        }
        private string  _BAAUFTYP="0";
        [Column]
        public string BAAUFTYP
        {
            get
            {
                return _BAAUFTYP;
            }
            set
            {
                if (value != _BAAUFTYP)
                {
                     _BAAUFTYP = value;
                    NotifyPropertyChanged("BAAUFTYP");
                }
            }
        }
        private string  _BAAUFKRS="0";
        [Column]
        public string BAAUFKRS
        {
            get
            {
                return _BAAUFKRS;
            }
            set
            {
                if (value != _BAAUFKRS)
                {
                     _BAAUFKRS = value;
                    NotifyPropertyChanged("BAAUFKRS");
                }
            }
        }
        private string  _BAAUFLFD="0";
        [Column]
        public string BAAUFLFD
        {
            get
            {
                return _BAAUFLFD;
            }
            set
            {
                if (value != _BAAUFLFD)
                {
                     _BAAUFLFD = value;
                    NotifyPropertyChanged("BAAUFLFD");
                }
            }
        }
        private string  _BAAUFPOS="0";
        [Column]
        public string BAAUFPOS
        {
            get
            {
                return _BAAUFPOS;
            }
            set
            {
                if (value != _BAAUFPOS)
                {
                     _BAAUFPOS = value;
                    NotifyPropertyChanged("BAAUFPOS");
                }
            }
        }
        private string  _BASA="0";
        [Column]
        public string BASA
        {
            get
            {
                return _BASA;
            }
            set
            {
                if (value != _BASA)
                {
                     _BASA = value;
                    NotifyPropertyChanged("BASA");
                }
            }
        }
        private string  _BAPOS="0";
        [Column]
        public string BAPOS
        {
            get
            {
                return _BAPOS;
            }
            set
            {
                if (value != _BAPOS)
                {
                     _BAPOS = value;
                    NotifyPropertyChanged("BAPOS");
                }
            }
        }
        private string  _BAAFOLGE="0";
        [Column]
        public string BAAFOLGE
        {
            get
            {
                return _BAAFOLGE;
            }
            set
            {
                if (value != _BAAFOLGE)
                {
                     _BAAFOLGE = value;
                    NotifyPropertyChanged("BAAFOLGE");
                }
            }
        }
        private string  _BADATVON="0";
        [Column]
        public string BADATVON
        {
            get
            {
                return _BADATVON;
            }
            set
            {
                if (value != _BADATVON)
                {
                     _BADATVON = value;
                    NotifyPropertyChanged("BADATVON");
                }
            }
        }
        private string  _BAKART="0";
        [Column]
        public string BAKART
        {
            get
            {
                return _BAKART;
            }
            set
            {
                if (value != _BAKART)
                {
                     _BAKART = value;
                    NotifyPropertyChanged("BAKART");
                }
            }
        }
        private string  _BAKVAR="0";
        [Column]
        public string BAKVAR
        {
            get
            {
                return _BAKVAR;
            }
            set
            {
                if (value != _BAKVAR)
                {
                     _BAKVAR = value;
                    NotifyPropertyChanged("BAKVAR");
                }
            }
        }
        private string  _BAKFARBE="0";
        [Column]
        public string BAKFARBE
        {
            get
            {
                return _BAKFARBE;
            }
            set
            {
                if (value != _BAKFARBE)
                {
                     _BAKFARBE = value;
                    NotifyPropertyChanged("BAKFARBE");
                }
            }
        }
        private string  _BAMTR1="0";
        [Column]
        public string BAMTR1
        {
            get
            {
                return _BAMTR1;
            }
            set
            {
                if (value != _BAMTR1)
                {
                     _BAMTR1 = value;
                    NotifyPropertyChanged("BAMTR1");
                }
            }
        }
        private string  _BAANZ1="0";
        [Column]
        public string BAANZ1
        {
            get
            {
                return _BAANZ1;
            }
            set
            {
                if (value != _BAANZ1)
                {
                     _BAANZ1 = value;
                    NotifyPropertyChanged("BAANZ1");
                }
            }
        }
        private string  _BAMTR2="0";
        [Column]
        public string BAMTR2
        {
            get
            {
                return _BAMTR2;
            }
            set
            {
                if (value != _BAMTR2)
                {
                     _BAMTR2 = value;
                    NotifyPropertyChanged("BAMTR2");
                }
            }
        }
        private string  _BAANZ2="0";
        [Column]
        public string BAANZ2
        {
            get
            {
                return _BAANZ2;
            }
            set
            {
                if (value != _BAANZ2)
                {
                     _BAANZ2 = value;
                    NotifyPropertyChanged("BAANZ2");
                }
            }
        }
        private string  _BAMTR3="0";
        [Column]
        public string BAMTR3
        {
            get
            {
                return _BAMTR3;
            }
            set
            {
                if (value != _BAMTR3)
                {
                     _BAMTR3 = value;
                    NotifyPropertyChanged("BAMTR3");
                }
            }
        }
        private string  _BAANZ3="0";
        [Column]
        public string BAANZ3
        {
            get
            {
                return _BAANZ3;
            }
            set
            {
                if (value != _BAANZ3)
                {
                     _BAANZ3 = value;
                    NotifyPropertyChanged("BAANZ3");
                }
            }
        }
        private string  _BAMTR4="0";
        [Column]
        public string BAMTR4
        {
            get
            {
                return _BAMTR4;
            }
            set
            {
                if (value != _BAMTR4)
                {
                     _BAMTR4 = value;
                    NotifyPropertyChanged("BAMTR4");
                }
            }
        }
        private string  _BAANZ4="0";
        [Column]
        public string BAANZ4
        {
            get
            {
                return _BAANZ4;
            }
            set
            {
                if (value != _BAANZ4)
                {
                     _BAANZ4 = value;
                    NotifyPropertyChanged("BAANZ4");
                }
            }
        }
        private string  _BAMTR5="0";
        [Column]
        public string BAMTR5
        {
            get
            {
                return _BAMTR5;
            }
            set
            {
                if (value != _BAMTR5)
                {
                     _BAMTR5 = value;
                    NotifyPropertyChanged("BAMTR5");
                }
            }
        }
        private string  _BAANZ5="0";
        [Column]
        public string BAANZ5
        {
            get
            {
                return _BAANZ5;
            }
            set
            {
                if (value != _BAANZ5)
                {
                     _BAANZ5 = value;
                    NotifyPropertyChanged("BAANZ5");
                }
            }
        }
        private string  _BAMTR6="0";
        [Column]
        public string BAMTR6
        {
            get
            {
                return _BAMTR6;
            }
            set
            {
                if (value != _BAMTR6)
                {
                     _BAMTR6 = value;
                    NotifyPropertyChanged("BAMTR6");
                }
            }
        }
        private string  _BAMTR7="0";
        [Column]
        public string BAMTR7
        {
            get
            {
                return _BAMTR7;
            }
            set
            {
                if (value != _BAMTR7)
                {
                     _BAMTR7 = value;
                    NotifyPropertyChanged("BAMTR7");
                }
            }
        }
        private string  _BAANZ7="0";
        [Column]
        public string BAANZ7
        {
            get
            {
                return _BAANZ7;
            }
            set
            {
                if (value != _BAANZ7)
                {
                     _BAANZ7 = value;
                    NotifyPropertyChanged("BAANZ7");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<BAAUF> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\BAAUF\\BAAUF" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\BAAUF" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("BAAUF");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (BAAUF row in ListInput)
                {
                    XElement f = new XElement("BAAUF");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement BAMANDANT = new XElement("BAMANDANT");
                         BAMANDANT.Value = row.BAMANDANT;
                          f.Add(BAMANDANT);
                        XElement BAAUFTYP = new XElement("BAAUFTYP");
                         BAAUFTYP.Value = row.BAAUFTYP;
                          f.Add(BAAUFTYP);
                        XElement BAAUFKRS = new XElement("BAAUFKRS");
                         BAAUFKRS.Value = row.BAAUFKRS;
                          f.Add(BAAUFKRS);
                        XElement BAAUFLFD = new XElement("BAAUFLFD");
                         BAAUFLFD.Value = row.BAAUFLFD;
                          f.Add(BAAUFLFD);
                        XElement BAAUFPOS = new XElement("BAAUFPOS");
                         BAAUFPOS.Value = row.BAAUFPOS;
                          f.Add(BAAUFPOS);
                        XElement BASA = new XElement("BASA");
                         BASA.Value = row.BASA;
                          f.Add(BASA);
                        XElement BAPOS = new XElement("BAPOS");
                         BAPOS.Value = row.BAPOS;
                          f.Add(BAPOS);
                        XElement BAAFOLGE = new XElement("BAAFOLGE");
                         BAAFOLGE.Value = row.BAAFOLGE;
                          f.Add(BAAFOLGE);
                        XElement BADATVON = new XElement("BADATVON");
                         BADATVON.Value = row.BADATVON;
                          f.Add(BADATVON);
                        XElement BAKART = new XElement("BAKART");
                         BAKART.Value = row.BAKART;
                          f.Add(BAKART);
                        XElement BAKVAR = new XElement("BAKVAR");
                         BAKVAR.Value = row.BAKVAR;
                          f.Add(BAKVAR);
                        XElement BAKFARBE = new XElement("BAKFARBE");
                         BAKFARBE.Value = row.BAKFARBE;
                          f.Add(BAKFARBE);
                        XElement BAMTR1 = new XElement("BAMTR1");
                         BAMTR1.Value = row.BAMTR1;
                          f.Add(BAMTR1);
                        XElement BAANZ1 = new XElement("BAANZ1");
                         BAANZ1.Value = row.BAANZ1;
                          f.Add(BAANZ1);
                        XElement BAMTR2 = new XElement("BAMTR2");
                         BAMTR2.Value = row.BAMTR2;
                          f.Add(BAMTR2);
                        XElement BAANZ2 = new XElement("BAANZ2");
                         BAANZ2.Value = row.BAANZ2;
                          f.Add(BAANZ2);
                        XElement BAMTR3 = new XElement("BAMTR3");
                         BAMTR3.Value = row.BAMTR3;
                          f.Add(BAMTR3);
                        XElement BAANZ3 = new XElement("BAANZ3");
                         BAANZ3.Value = row.BAANZ3;
                          f.Add(BAANZ3);
                        XElement BAMTR4 = new XElement("BAMTR4");
                         BAMTR4.Value = row.BAMTR4;
                          f.Add(BAMTR4);
                        XElement BAANZ4 = new XElement("BAANZ4");
                         BAANZ4.Value = row.BAANZ4;
                          f.Add(BAANZ4);
                        XElement BAMTR5 = new XElement("BAMTR5");
                         BAMTR5.Value = row.BAMTR5;
                          f.Add(BAMTR5);
                        XElement BAANZ5 = new XElement("BAANZ5");
                         BAANZ5.Value = row.BAANZ5;
                          f.Add(BAANZ5);
                        XElement BAMTR6 = new XElement("BAMTR6");
                         BAMTR6.Value = row.BAMTR6;
                          f.Add(BAMTR6);
                        XElement BAMTR7 = new XElement("BAMTR7");
                         BAMTR7.Value = row.BAMTR7;
                          f.Add(BAMTR7);
                        XElement BAANZ7 = new XElement("BAANZ7");
                         BAANZ7.Value = row.BAANZ7;
                          f.Add(BAANZ7);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
